package com.musemun.controller;

import com.musemun.common.CommonResult;
import com.musemun.common.DataVerification;
import com.musemun.common.ResultUtil;
import com.musemun.pojo.Crlendifo;
import com.musemun.pojo.Cropifo;
import com.musemun.pojo.Lendcontact;
import com.musemun.pojo.Vo.CrFixLendQueryTimeVo;
import com.musemun.pojo.Vo.CrLendVo;
import com.musemun.service.function.LendContact;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/crms/crlendAdmin")
@Api(tags="文物外借模块")
public class LendModuleController {

    @Resource
    private LendContact lendContact;

    /**
     *
     * @param crLendVo crlendifo 文物外借详情表
     * cropifo  文物操作记录表
     * lendcontact 外借联系人表
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "添加文物外借信息", httpMethod = "POST")
    public CommonResult insert(@Valid @RequestBody CrLendVo crLendVo, BindingResult bindingResult) {
        DataVerification.check(bindingResult);
        boolean flag = lendContact.insert(crLendVo.getCrlendifo(), crLendVo.getLendcontact(), crLendVo.getCropifo());
        Map<String, Boolean> result = new HashMap<>();
        result.put("flag", flag);
        return ResultUtil.success(result);
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除外借信息", httpMethod = "GET")
    public CommonResult delete(@RequestParam(value = "crNo", required = false) String crNo) {
        long crNoL = DataVerification.checkNumLong(crNo);
        boolean flag = lendContact.delete(crNoL);
        Map<String, Boolean> result = new HashMap<>();
        result.put("flag", flag);
        return ResultUtil.success(result);
    }

    @GetMapping("/update")
    @ApiOperation(value = "返回要修改的信息(GET)", httpMethod = "GET")
    public CommonResult update(@RequestParam(value = "crNo", required = false) String crNo) {
//        long crNoL = DataVerification.checkNumLong(crNo);
//
//        return ResultUtil.success(lendContact.update(crNoL));
        return new CommonResult(-1, "禁止访问", "该接口已经停止访问");
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改外借信息(POST)", httpMethod = "POST")
    public CommonResult update() {
//        DataVerification.check(bindingResult);
//        boolean flag = lendContact.update(crLendVo.getCrlendifo(), crLendVo.getLendcontact());
//        Map<String, Boolean> result = new HashMap<>();
//        result.put("flag", flag);
//        return ResultUtil.success(result);
        return new CommonResult(-1, "禁止访问", "该接口已经停止访问");
    }

    @GetMapping("/selectAll")
    @ApiOperation(value = "无条件查询(可分页)", httpMethod = "GET")
    public CommonResult selectAll(@RequestParam(value = "pageNum", required = false) String pageNum,
                                  @RequestParam(value = "pageSize", required = false) String pageSize) {
        int pageNumI = DataVerification.checkNumInteger(pageNum);
        int pageSizeI = DataVerification.checkNumInteger(pageSize);
        Map<String, Object> result = new HashMap<>();
        return ResultUtil.success(lendContact.selectAll(pageNumI, pageSizeI));

    }

    @GetMapping("/selectByName")
    @ApiOperation(value = "根据外借人查询(可分页)", httpMethod = "GET")
    public CommonResult selectByName(@RequestParam(value = "FCName") String FCName,
                                     @RequestParam(value = "pageNum", required = false) String pageNum,
                                     @RequestParam(value = "pageSize", required = false) String pageSize) {
        int pageNumI = DataVerification.checkNumInteger(pageNum);
        int pageSizeI = DataVerification.checkNumInteger(pageSize);
        Map<String, Object> result = new HashMap<>();
        return ResultUtil.success(lendContact.findByName(FCName, pageNumI, pageSizeI));
    }

    @GetMapping("/selectById")
    @ApiOperation(value = "根据文物编号(可分页)", httpMethod = "GET")
    public CommonResult selectById(@RequestParam(value = "crNo", required = false) String crNo) {
        Long crNoL = DataVerification.checkNumLong(crNo);
        return ResultUtil.success(lendContact.findById(crNoL));
    }

    @PostMapping("/selectByTime")
    @ApiOperation(value = "根据外借起始时间查询", httpMethod = "POST")
    public CommonResult selectByTime(@Validated @RequestBody CrFixLendQueryTimeVo crFixLendQueryTimeVo, BindingResult bindingResult) {
        DataVerification.check(bindingResult);
        List list = lendContact.findByTime(crFixLendQueryTimeVo.getLeftTime(), crFixLendQueryTimeVo.getRightTime(), crFixLendQueryTimeVo.getOperate());
        Map<String, Object> result = new HashMap<>();
        result.put("flag", true);
        result.put("list", list);
        return ResultUtil.success(result);
    }

}
